<?php

namespace App\Models\News;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use App\Models\AdminUser;
use App\Models\Artists\Artist;
class Video extends Model
{
    //
    use SoftDeletes;
	protected $connection = 'mysql_news';
	
	protected $appends = ['urls'];
	
	public static function boot() {
        parent::boot();
        static::saving(function($model) {
			$model->admin_edit_user_id = Auth::guard('admin')->user()->id;
        });
		static::creating(function($model) {
			//创建唯一标识UUID
			$model->unique = create_uuid();
			$model->admin_user_id = Auth::guard('admin')->user()->id;
        });
    }
	
	public function getUrlsAttribute()
	{
		$url = env('MAIN_DOMAIN').'/video.html?id='.$this->unique;
		return '<iframe src="'.$url.'" frameborder=0 allowfullscreen></iframe>';
	}
	
	
	public function lists()
	{
		return $this->hasMany(AtlasList::class, 'videos_id', 'id');
	}
	
	 //栏目名称
    public function news_class()
    {
        return $this->hasOne(NewsClass::class,'id','class_id');
    }
	//发布人
	public function admin_user()
	{
		return $this->hasOne(AdminUser::class, 'id', 'admin_user_id');
	}
	//最后更新人
	public function admin_edit_user()
	{
		return $this->hasOne(AdminUser::class, 'id', 'admin_edit_user_id');
	}

	//艺术家
	public function artists()
    {
        return $this->belongsToMany(Artist::class, 'videos_artists', 'video_id');
    }
    //关键词
	public function tags()
    {
        return $this->belongsToMany(Tag::class, 'videos_tags', 'video_id');
    }
}
